/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Processing;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * 
 */
public class Util
{
    public static final int StandardDock = Graphics.TOP | Graphics.LEFT;

    public static String[] Split(String s, String p)
    {
        Vector arrTemp = new Vector();
        int soChuoi = 1;
        int idx = 0;
        while(s.indexOf(p, idx) > -1)
        {
            int idxtemp = s.indexOf(p, idx);
            arrTemp.addElement(s.substring(idx, idxtemp));
            idx = idxtemp + 1;
            if(idx >= s.length())
            {
                break;
            }
        }
        if(idx < s.length())
        {
            arrTemp.addElement(s.substring(idx, s.length()));
        }
        String[] kq = new String[arrTemp.size()];
        for(int i=0; i<kq.length; ++i)
        {
            kq[i] = (String)arrTemp.elementAt(i);
        }
        return kq;
    }

    public static Image CreateBoard(Hashtable dsQC, Image[] listImg, int dai, int cao, int ktOCo, int soOCo)
    {
        Image kq = Image.createImage(dai, cao);
        Graphics g = kq.getGraphics();
        g.drawImage(listImg[0], 0, 0, StandardDock);
        Enumeration en = dsQC.keys();
        while(en.hasMoreElements())
        {
            Integer each = (Integer)en.nextElement();
            int indexHinh = ((Integer)dsQC.get(each)).intValue();
            Mark vtve = ConvertCord2Pos(ktOCo, each.intValue(), soOCo);
            g.drawImage(listImg[indexHinh], vtve.X, vtve.Y, StandardDock);
            /*g.drawRegion(listImg[indexHinh], 0, 0, listImg[indexHinh].getWidth(),
                    listImg[indexHinh].getHeight(), Sprite.TRANS_NONE, vtve.X, vtve.Y, 0);*/
        }
        return kq;
    }

    public static Image CreateBoardWithSelected(Hashtable dsQC, Image[] listImg, int dai, int cao, int ktOCo, int soOCo, int vtX, int vtY, int csHinhSelect, boolean thTren)
    {
        Image kq = Image.createImage(dai, cao);
        Graphics g = kq.getGraphics();
        g.drawImage(listImg[0], 0, 0, StandardDock);
        if(!thTren)
        {
            g.drawImage(listImg[csHinhSelect], vtX * ktOCo, vtY * ktOCo, StandardDock);
        }
        Enumeration en = dsQC.keys();
        while(en.hasMoreElements())
        {
            Integer each = (Integer)en.nextElement();
            int indexHinh = ((Integer)dsQC.get(each)).intValue();
            Mark vtve = ConvertCord2Pos(ktOCo, each.intValue(), soOCo);
            g.drawImage(listImg[indexHinh], vtve.X, vtve.Y, StandardDock);
            /*g.drawRegion(listImg[indexHinh], 0, 0, listImg[indexHinh].getWidth(),
                    listImg[indexHinh].getHeight(), Sprite.TRANS_NONE, vtve.X, vtve.Y, 0);*/
        }
        if(thTren)
        {
            g.drawImage(listImg[csHinhSelect], vtX * ktOCo, vtY * ktOCo, StandardDock);
        }
        return kq;
    }

    public static Image CreateMovableList(Hashtable[] dsQC, Image[] dsImg, int csHinhNext, boolean thTren, int dai, int cao, int ktOCo, int soOCo)
    {
        Image kq = CreateBoard(dsQC[0], dsImg, dai, cao, ktOCo, soOCo);
        Graphics g = kq.getGraphics();
        Mark vtve = new Mark();
        Enumeration en = dsQC[2].keys();
        while (en.hasMoreElements())
        {
            Integer each = (Integer)en.nextElement();
            int qc = ((Integer)dsQC[2].get(each)).intValue();
            vtve = ConvertCord2Pos(ktOCo, each.intValue(), soOCo);
            g.drawImage(dsImg[qc], vtve.X, vtve.Y, StandardDock);
        }

        Vector dsND = (Vector)dsQC[1].get("dsNC");
        if (thTren)
        {
            en = dsQC[1].keys();
            while (en.hasMoreElements())
            {
                Object each = en.nextElement();
                try
                {
                    Integer vt = (Integer)each;
                    Integer qc = (Integer)dsQC[1].get(vt);
                    vtve = ConvertCord2Pos(ktOCo, vt.intValue(), soOCo);
                    g.drawImage(dsImg[qc.intValue()], vtve.X, vtve.Y, StandardDock);
                }
                catch (Exception ex)
                {
                }
            }
            Enumeration enTemp = dsND.elements();
            while(enTemp.hasMoreElements())
            {
                int each = ((Integer)enTemp.nextElement()).intValue();
                vtve = ConvertCord2Pos(ktOCo, each, soOCo);
                g.drawImage(dsImg[csHinhNext], vtve.X, vtve.Y, StandardDock);
            }
        }
        else
        {
            Enumeration enTemp = dsND.elements();
            while(enTemp.hasMoreElements())
            {
                int each = ((Integer)enTemp.nextElement()).intValue();
                vtve = ConvertCord2Pos(ktOCo, each, soOCo);
                g.drawImage(dsImg[csHinhNext], vtve.X, vtve.Y, StandardDock);
            }

            en = dsQC[1].keys();
            while (en.hasMoreElements())
            {
                Object each = en.nextElement();
                try
                {
                    Integer vt = (Integer)each;
                    Integer qc = (Integer)dsQC[1].get(vt);
                    vtve = ConvertCord2Pos(ktOCo, vt.intValue(), soOCo);
                    g.drawImage(dsImg[qc.intValue()], vtve.X, vtve.Y, StandardDock);
                }
                catch (Exception ex)
                {
                }
            }
        }

        return kq;
    }

    public static Image CreateMovableListWithSelect(Hashtable[] dsQC, Image[] dsImg, int csHinhNext, boolean thTren, int dai, int cao, int ktOCo, int soOCo, int vtX, int vtY, int csHinhSelect)
    {
        Image kq;
        
        if(!thTren)
        {
            kq = CreateBoardWithSelected(dsQC[0], dsImg, dai, cao, ktOCo, soOCo, vtX, vtY, csHinhSelect, thTren);
        }
        else
        {
            kq = CreateBoard(dsQC[0], dsImg, dai, cao, ktOCo, soOCo);
        }
        //kq = CreateBoard(dsQC[0], dsImg, dai, cao, ktOCo, soOCo);
        Graphics g = kq.getGraphics();
        Mark vtve = new Mark();
        Enumeration en = dsQC[2].keys();
        while (en.hasMoreElements())
        {
            Integer each = (Integer)en.nextElement();
            int qc = ((Integer)dsQC[2].get(each)).intValue();
            vtve = ConvertCord2Pos(ktOCo, each.intValue(), soOCo);
            g.drawImage(dsImg[qc], vtve.X, vtve.Y, StandardDock);
        }

        Vector dsND = (Vector)dsQC[1].get("dsNC");
        if (thTren)
        {
            en = dsQC[1].keys();
            while (en.hasMoreElements())
            {
                Object each = en.nextElement();
                try
                {
                    Integer vt = (Integer)each;
                    Integer qc = (Integer)dsQC[1].get(vt);
                    vtve = ConvertCord2Pos(ktOCo, vt.intValue(), soOCo);
                    g.drawImage(dsImg[qc.intValue()], vtve.X, vtve.Y, StandardDock);
                }
                catch (Exception ex)
                {
                }
            }
            Enumeration enTemp = dsND.elements();
            while(enTemp.hasMoreElements())
            {
                int each = ((Integer)enTemp.nextElement()).intValue();
                vtve = ConvertCord2Pos(ktOCo, each, soOCo);
                g.drawImage(dsImg[csHinhNext], vtve.X, vtve.Y, StandardDock);
            }
            g.drawImage(dsImg[csHinhSelect], vtX * ktOCo, vtY * ktOCo, StandardDock);
        }
        else
        {
            Enumeration enTemp = dsND.elements();
            while(enTemp.hasMoreElements())
            {
                int each = ((Integer)enTemp.nextElement()).intValue();
                if(each != vtY * soOCo + vtX)
                {
                    vtve = ConvertCord2Pos(ktOCo, each, soOCo);
                    g.drawImage(dsImg[csHinhNext], vtve.X, vtve.Y, StandardDock);
                }
            }

            en = dsQC[1].keys();
            while (en.hasMoreElements())
            {
                Object each = en.nextElement();
                try
                {
                    Integer vt = (Integer)each;
                    Integer qc = (Integer)dsQC[1].get(vt);
                    vtve = ConvertCord2Pos(ktOCo, vt.intValue(), soOCo);
                    g.drawImage(dsImg[qc.intValue()], vtve.X, vtve.Y, StandardDock);
                }
                catch (Exception ex)
                {
                }
            }
        }

        return kq;
    }

    public static Mark ConvertCord2Pos(int ktQC, int vt, int soOCo)
    {
        Mark kq = new Mark();
        int y = vt / soOCo;
        int x = vt % soOCo;
        kq.X = x * ktQC;
        kq.Y = y * ktQC;
        return kq;
    }

    public static Mark GetBoardPos(int x, int y, int stX, int stY, int w, int h, int sW, int sH)
    {
        Mark kq = new Mark();
        x -= stX;
        y -= stY;
        if (x < 0 || x > sW || y < 0 || y > sH)
        {
            kq.X = -1;
            return kq;
        }

        kq.X = y / h;
        kq.Y = x / w;

        return kq;
    }
}
